/*
 * VirtualFile.h
 *
 *  Created on: Jul 23, 2009
 *      Author: kurt
 */

#ifndef VIRTUALFILE_H_
#define VIRTUALFILE_H_

#include "Object.h"

namespace objdraw 
{

class VirtualFile: public objdraw::Object 
{		
	public:
		typedef struct virtualFileFunc
		{
			bool (*open)(const char * fname, const char * mode, void *& stream);
			void (*close)(void *& stream);
			
			int (*flush)(void * stream);
			int (*seek)(void * stream, long offset, int origin);
			bool (*eof)(void * stream);
			long (*tell)(void * stream);
			
			int (*read)(void * buffer, size_t size, size_t num, void * stream);
			int (*write)(const void *buffer, size_t size, size_t count, void *stream);
			
			bool (*error)(void * stream);
			std::string (*geterror)(void * stream);
			
			bool (*is_open)(void * stream);
		} VirtualFileFunc;
	protected:
		virtual ~VirtualFile();
		VirtualFileFunc funcs;
		void * stream;
	public:
		virtual inline void Delete() { delete this; }
		
		VirtualFile();
		VirtualFile(VirtualFileFunc ops);
		VirtualFile(VirtualFileFunc ops, void * _stream);
		
		VirtualFile(const char * fname, const char * mode);
		
		bool open(const char * fname, const char * mode);
		void close();
		
		bool eof();
		int flush();
		
		int read(void * buffer, size_t size, size_t num);
		int write(const void *buffer, size_t size, size_t count);
		
		int seek(long offset, int origin);
		long tell();
		
		bool error();
		std::string getError();
		
		bool isOpen();
		
		virtual std::string toString();
};

}

#endif /* VIRTUALFILE_H_ */
